package com.hue.gulimail.gulimailauth.web;

import com.alibaba.fastjson.TypeReference;
import com.hue.common.constant.AuthConstant;
import com.hue.common.utils.R;
import com.hue.common.vo.UserVo;
import com.hue.gulimail.gulimailauth.feign.MemberFeignService;
import com.hue.gulimail.gulimailauth.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @Author: 姚凤桥
 * @Date: 2021/11/23 21:35
 * @Description:
 */
@Controller
public class LoginController {
    @Autowired
    private MemberFeignService memberFeignService;

    @PostMapping("/login")
    public String login(LoginVo loginVo, RedirectAttributes redirectAttributes, HttpSession httpSession) {
        R r = memberFeignService.login(loginVo);

        if ((Integer) r.get("code") == 0) {
            UserVo userVo = r.getData(new TypeReference<UserVo>() {
            });
            /**
             * 在引入了SpringSession后，存储到session中的数据会被默认保存到redis中
             * session中存储的数据默认作用域只在当前域名
             */
            httpSession.setAttribute(AuthConstant.LOGIN_USER, userVo);
            return "redirect:http://gulimail.com";
        }

        redirectAttributes.addFlashAttribute("error", r.get("msg"));
        return "redirect:http://auth.gulimail.com";
    }
}
